package com.cloudlead.customerAction.service.impl;

import com.cloudlead.common.lang.persistence.*;
import com.cloudlead.commonData.service.*;
import com.cloudlead.commonData.vo.*;
import com.cloudlead.customerAction.service.GpsSer;
import com.cloudlead.customerAction.service.OrderTrackingSer;
import com.cloudlead.customerService.vo.ContainerCountVo;
import com.cloudlead.customerService.vo.CustmoerOrderVo;
import com.cloudlead.customerService.vo.GPSVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * Created by 20150817 on 2015/8/31.
 */
@Service
public class OrderTrackingSerImpl implements OrderTrackingSer{
    @Autowired
    TbCustomerOrderService tbCustomerOrderService;
    @Autowired
    BaseVoyageService baseVoyageService;
    @Autowired
    TbContainerInfoService tbContainerInfoService;
    @Autowired
    TbFleetOrderService tbFleetOrderService;
    @Autowired
    GpsSer gpsSer;
    @Autowired
    BaseCarService baseCarService;
    @Override
    public CustmoerOrderVo findCustomerOrderByTrackingNo(String trackingNo) {

        System.out.print("------------------" + trackingNo);

        TbContainerInfo tbContainerInfo=tbContainerInfoService.findOne(new Filter(new Criteria(new Term("containerNo", trackingNo))));//按箱号查询
        if(tbContainerInfo!=null)//箱对象
            trackingNo=tbContainerInfo.getTbCustomerOrder().getId();//

        CustmoerOrderVo customerOrder= new CustmoerOrderVo();

        List<Term> tr = new ArrayList<Term>();//and 条件
        tr.add(new Term("bussinessCode", trackingNo));//运单号


        Filter filter=new Filter(new Criteria(tr));
        TbCustomerOrder tbCustomerOrder=tbCustomerOrderService.findOne(filter);//按运单号查询货主订单

        if(tbCustomerOrder==null){//按运单号未查到 按订单号查

            List<Term> tr2 = new ArrayList<Term>();//and 条件
            tr2.add(new Term("id", trackingNo));//订单号
            tbCustomerOrder=tbCustomerOrderService.findOne(new Filter(new Criteria(tr2)));//查询货主订单
        }

        if(tbCustomerOrder!=null) {

            //起始港车队订单
            List<Term> startFleetOrderTr = new ArrayList<Term>();//and 条件
            startFleetOrderTr.add(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()));
            startFleetOrderTr.add(new Term("isStart","1"));
            List<TbFleetOrder> startFleetOrderList=tbFleetOrderService.list(new Filter(new Criteria(startFleetOrderTr)),new Sort());

            //目的港车队订单
            List<Term> destFleeTr = new ArrayList<Term>();//and 条件
            destFleeTr.add(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()));
            destFleeTr.add(new Term("isStart","0"));
            List<TbFleetOrder> destFleetOrderList=tbFleetOrderService.list(new Filter(new Criteria(destFleeTr)), new Sort());

            if(startFleetOrderList.size()>0&&destFleetOrderList.size()>0){
                customerOrder.setTerms("DR-DR");
                customerOrder.setStartFleetName(startFleetOrderList.get(0).getFleetCompany().getCompanyName());
                customerOrder.setDestFleetName(destFleetOrderList.get(0).getFleetCompany().getCompanyName());

            }else if (startFleetOrderList.size()>0){
                customerOrder.setTerms("DR-CY");
                customerOrder.setStartFleetName(startFleetOrderList.get(0).getFleetCompany().getCompanyName());
            }else if (destFleetOrderList.size()>0){
                customerOrder.setTerms("CY-DR");
                customerOrder.setDestFleetName(destFleetOrderList.get(0).getFleetCompany().getCompanyName());
            }else {
                customerOrder.setTerms("CY-CY");
            }
            BaseVoyage baseVoyage = baseVoyageService.findOne(tbCustomerOrder.getBaseVoya());//查询航次信息

            //赋值
            customerOrder.setGoodsName(tbCustomerOrder.getCommodity());//货物名称
            customerOrder.setGoodsType("一般");//货物类型
            customerOrder.setContainerType(tbCustomerOrder.getQuantity());//箱型
            customerOrder.setId(tbCustomerOrder.getId());//订单号
            customerOrder.setBussinessCode(tbCustomerOrder.getBussinessCode());//运单号
            customerOrder.setCommodity(tbCustomerOrder.getCommodity());//货物名称
            customerOrder.setOrdertime(tbCustomerOrder.getOrdertime());//创建时间
            customerOrder.setBaseCompany(tbCustomerOrder.getBaseCompany()!=null?tbCustomerOrder.getBaseCompany().getCompanyName():"无");//客户名称
            customerOrder.setQuantity(tbCustomerOrder.getQuantity());//箱量
            customerOrder.setVoyaeCode(baseVoyage.getVoyage());//航次名称
            customerOrder.setVoyaeDestPortName(tbCustomerOrder.getDelName());//航次目的港
            customerOrder.setVoyaeShipName(baseVoyage.getBaseShip()!=null?baseVoyage.getBaseShip().getName():"无");//航次船舶名称
            customerOrder.setVoyaeStartPortName(tbCustomerOrder.getPolName());//航次起始港
            return customerOrder;
        }else{
            return  null;
        }
    }

    @Override
    public   List<ContainerCountVo>  findContainerByOrder(String trackingNo) {

        TbContainerInfo tbContainerInfo=tbContainerInfoService.findOne(new Filter(new Criteria(new Term("containerNo", trackingNo))));//按箱号查询

        if(tbContainerInfo!=null)//箱对象
            trackingNo=tbContainerInfo.getTbCustomerOrder().getId();//

        TbCustomerOrder tbCustomerOrder = tbCustomerOrderService.findOne( new Filter(new Criteria(new Term("bussinessCode", trackingNo))));//按运单号查询货主订单
        if (tbCustomerOrder == null) //按运单号未查到 按订单号查
            tbCustomerOrder = tbCustomerOrderService.findOne(new Filter(new Criteria(new Term("id", trackingNo))));//查询货主订单


        List<TbContainerInfo> startList = null;//起始港车队箱集合
        List<TbContainerInfo> destList = null;//目的港车队箱集合
        List<TbContainerInfo> shipList = null;//货主箱集合
        String terms = "";//条款

        //起始港车队订单
        List<Term> startFleetOrderTr = new ArrayList<Term>();//and 条件
        startFleetOrderTr.add(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()));
        startFleetOrderTr.add(new Term("isStart","1"));
        List<TbFleetOrder> startFleetOrderList=tbFleetOrderService.list(new Filter(new Criteria(startFleetOrderTr)),new Sort());

        //目的港车队订单
        List<Term> destFleeTr = new ArrayList<Term>();//and 条件
        destFleeTr.add(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()));
        destFleeTr.add(new Term("isStart","0"));
        List<TbFleetOrder> destFleetOrderList=tbFleetOrderService.list(new Filter(new Criteria(destFleeTr)),new Sort());

        List startFleetOrderListNew = new ArrayList();
        List destFleetOrderListNew = new ArrayList();
        for(TbFleetOrder fleetOrder: startFleetOrderList){
            startFleetOrderListNew.add(fleetOrder.getId());
        }
        for(TbFleetOrder fleetOrder: destFleetOrderList){
            destFleetOrderListNew.add(fleetOrder.getId());
        }

        List<ContainerCountVo> containerList = new ArrayList<ContainerCountVo>();

        if(startFleetOrderListNew.size()>0 && destFleetOrderListNew.size()>0){
            //起始港车队箱集合
            startList = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id",Operator.IN,startFleetOrderListNew))), new Sort());
            //目的港车队箱集合
            destList = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id",Operator.IN,destFleetOrderListNew))), new Sort());
            terms="DR-DR";
        }else  if (startFleetOrderListNew.size()>0){
            //起始港车队箱集合
            startList = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id",Operator.IN,startFleetOrderListNew))), new Sort());

            terms="DR-CY";
        }else  if (destFleetOrderListNew.size()>0){
            //目的港车队箱集合
            destList = tbContainerInfoService.list(new Filter(new Criteria(new Term("fleetOrder.id",Operator.IN,destFleetOrderListNew))), new Sort());
            terms="CY-DR";

        }else {
            //货主车队箱集合
            shipList = tbContainerInfoService.list(new Filter(new Criteria(new Term("tbCustomerOrder.id",tbCustomerOrder.getId()))), new Sort());

            terms="CY-CY";

        }


        if("DR-DR".equals(terms)){

            for (int i = 0; i < startList.size(); i++) {
                TbContainerInfo startContainer=startList.get(i);//起始港车队箱
                //返回数据
                ContainerCountVo containerVo=new ContainerCountVo();
                for (int j=0;j<destList.size();j++){
                    TbContainerInfo destContainer=destList.get(j);//目的港车队箱
                    if(destContainer!=null) {
                        if (startContainer.getContainerNo().equals(destContainer.getContainerNo())) {//匹配箱号

                            if (destContainer.getCar() != null) {
                                containerVo.setDestCarName(destContainer.getCar().getLicencePlate());//车牌号
                                if (destContainer.getCar() != null) {
                                    BaseCar car = baseCarService.view(destContainer.getCar().getId());//查询车辆
                                    containerVo.setDestGPS(car.getBaseG() != null ? car.getBaseG().getGpsName() : null);//gps名称

                                }
                            }
                            containerVo.setDestId(destContainer.getId());
                            containerVo.setDestFleetStatus(destContainer.getFleetStatus());//目的港车队箱状态

                        }
                    }
                }

                //起始港
                if(startContainer.getCar()!=null) {
                    containerVo.setStartCarName(startContainer.getCar().getLicencePlate());//车牌号
                    if(startContainer.getCar()!=null) {
                        BaseCar car = baseCarService.view(startContainer.getCar().getId());//查询车辆
                        containerVo.setDestGPS(car.getBaseG() != null ? car.getBaseG().getGpsName() : null);//gps名称
                    }
                }
                containerVo.setStartId(startContainer.getId());
                containerVo.setContainerNo(startContainer.getContainerNo());//箱号
                containerVo.setShipStatus(startContainer.getShipStatus());//船东箱状态
                containerVo.setStartFleetStatus(startContainer.getFleetStatus());//起始港车队箱状态
                containerList.add( containerVo);

            }
        }else if ("DR-CY".equals(terms)){

            for (int i = 0; i < startList.size(); i++) {
                TbContainerInfo container=startList.get(i);
                //返回数据
                ContainerCountVo containerVo=new ContainerCountVo();
                if(container.getCar()!=null) {
                    containerVo.setStartCarName(container.getCar().getLicencePlate());//车牌号
                    if(container.getCar()!=null) {
                        BaseCar car = baseCarService.view(container.getCar().getId());//查询车辆
                        containerVo.setDestGPS(car.getBaseG() != null ? car.getBaseG().getGpsName() : null);//gps名称
                    }
                }
                containerVo.setStartId(container.getId());
                containerVo.setContainerNo(container.getContainerNo());//箱号
                containerVo.setShipStatus(container.getShipStatus());//船东箱状态
                containerVo.setStartFleetStatus(container.getFleetStatus());//起始港车队箱状态
                containerList.add( containerVo);
            }

        }else  if ("CY-DR".equals(terms)){
            for (int i = 0; i < destList.size(); i++) {
                TbContainerInfo container=destList.get(i);
                //返回数据
                ContainerCountVo containerVo=new ContainerCountVo();
                if(container.getCar()!=null) {
                    containerVo.setDestCarName(container.getCar().getLicencePlate());//车牌号
                    if(container.getCar()!=null) {
                        BaseCar car = baseCarService.view(container.getCar().getId());//查询车辆
                        containerVo.setDestGPS(car.getBaseG() != null ? car.getBaseG().getGpsName() : null);//gps名称
                    }
                }
                containerVo.setDestId(container.getId());
                containerVo.setContainerNo(container.getContainerNo());//箱号
                containerVo.setShipStatus(container.getShipStatus());//船东箱状态
                containerVo.setDestFleetStatus(container.getFleetStatus());//目的港车队箱状态
                containerList.add( containerVo);
            }

        }else  if ("CY-CY".equals(terms)){
            for (int i = 0; i < shipList.size(); i++) {
                TbContainerInfo container=shipList.get(i);
                //返回数据
                ContainerCountVo containerVo=new ContainerCountVo();
                containerVo.setContainerNo(container.getContainerNo());//箱号
                containerVo.setShipStatus(container.getShipStatus());//船东箱状态
                containerList.add( containerVo);

            }
        }



        return containerList;





    }

    @Override
    public GPSVo findGPSByCarName(String carName, String gpsNmae) {
        Map  map= (Map) gpsSer.getGpsInfo(gpsNmae, carName);
        GPSVo gpsVo=new GPSVo();
        gpsVo.setCarName((String) map.get("carno"));
        gpsVo.setLatitude((String) map.get("lat"));
        gpsVo.setLongitude((String) map.get("lon"));
        gpsVo.setDate((String) map.get("recvtime"));
        gpsVo.setSpeed((String) map.get("speed"));
        gpsVo.setMsg((String) map.get("msg"));
        return gpsVo;
    }




}
